package com.borun.dictionary;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

import com.borun.bean.Base;
import com.borun.bean.Word;
import com.borun.dao.BaseDao;
import com.borun.dao.WordDao;
import com.borun.util.MapUtil;

public class App extends JFrame implements ActionListener, MouseListener {

	private static final long serialVersionUID = 1L;
	private BaseDao bDao = new BaseDao();
	private WordDao wDao = new WordDao();
	private JLabel lblTip;
	private Map<Integer, String> baseMap = new HashMap<>();
	private JComboBox<String> comboBox;
	private JPanel panelTop, panelBottom;
	private JButton btnEnter;
	private JMenuBar menuBar;
	private JMenu menu1, menu2, menu3;
	private JPopupMenu mainMenu;
	private JMenuItem addMenu, refreshMenu;
	private JMenuItem menuItem11, menuItem12, menuItem21, menuItem22,
			menuItem31, menuItem32;

	public App() {
		super("我爱记单词v1.0");

		menuBar = new JMenuBar();

		menu1 = new JMenu("系统管理(S)");
		menuItem11 = new JMenuItem("词库管理(A)  Ctrl+N");
		menuItem11.addActionListener(this);

		menuItem12 = new JMenuItem("打开(O)...  Ctrl+O");
		menuItem12.addActionListener(this);

		menu1.add(menuItem11);
		// menu1.add(menuItem12);
		menuBar.add(menu1);

		menu2 = new JMenu("编辑(E)");
		menuItem21 = new JMenuItem("新建(N)  Ctrl+N");
		menuItem21.addActionListener(this);
		menuItem22 = new JMenuItem("打开(O)...  Ctrl+O");
		menuItem21.addActionListener(this);
		menu2.add(menuItem21);
		menu2.add(menuItem22);
		// menuBar.add(menu2);

		menu3 = new JMenu("帮助(H)");
		menuItem31 = new JMenuItem("查看帮助(H)");
		menuItem31.addActionListener(this);
		menuItem32 = new JMenuItem("关于词典(A)");
		menuItem32.addActionListener(this);
		// menu3.add(menuItem31);
		menu3.add(menuItem32);
		menuBar.add(menu3);

		this.setJMenuBar(menuBar);
		this.setLayout(null);

		lblTip = new JLabel("请选择词库：");
		lblTip.setFont(new Font("", Font.BOLD, 20));
		lblTip.addMouseListener(this);
		// comboBox = new JComboBox<String>();
		// comboBox.setFont(new Font("", Font.BOLD, 20));

		panelTop = new JPanel();
		panelTop.add(lblTip);
		panelTop.add(getComboBox());
		panelTop.setBounds(20, 60, 300, 60);
		this.add(panelTop);

		btnEnter = new JButton("开始记单词(Enter)");

		panelBottom = new JPanel();
		btnEnter.setFont(new Font("", Font.BOLD, 20));
		btnEnter.addActionListener(this);
		panelBottom.add(btnEnter);
		panelBottom.setBounds(40, 140, 300, 60);

		this.add(panelBottom);

		this.setSize(400, 300);
		this.setLocationRelativeTo(null); // 设置窗口相对于指定组件的位置
		this.setResizable(false); // 禁止改变窗体大小
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

	// public static void main(String[] args) {
	// new App();
	// }

	private JComboBox<String> getComboBox() {
		if (comboBox == null) {
			comboBox = new JComboBox<String>();
			comboBox.setFont(new Font("", Font.BOLD, 20));			
			try {
				List<Base> bases = bDao.getBases();
				if (bases != null && bases.size() > 0) {
					for (Base b : bases) {
						baseMap.put(b.getBaseid(), b.getBasename());
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}

			for (Integer key : baseMap.keySet()) {
				comboBox.addItem(baseMap.get(key));
			}
		}
		return comboBox;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == menuItem32) {
			new About();
		} else if (e.getSource() == menuItem11) {
			new BaseList();
		} else if (e.getSource() == btnEnter) {

			if (comboBox.getItemCount() == 0) {			
				JOptionPane.showMessageDialog(null, "词库为空！请新建词库");
				return;
			}

			if (comboBox.getSelectedIndex() == -1) {
				JOptionPane.showMessageDialog(null, "抱歉！你还没有选择词库");
				return;
			}
			// 获取词库
			String selectedValue = (String) comboBox.getSelectedItem();
			int key = MapUtil.getKey(baseMap, selectedValue);

			try {
				List<Word> words = wDao.getWords(key);
				if (words.size() < 10) {
					JOptionPane.showMessageDialog(null,
							"当前词库的词量不足10个，请先添加单词或者切换词库！");
					return;
				}
				// this.setVisible(false);
				new ReciteWord(key);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} else if(e.getSource() == refreshMenu){
			this.setVisible(false);
			new App();
		}else if(e.getSource() == addMenu){
			new AddBase();
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getSource() == lblTip && e.isMetaDown()){
			mainMenu = new JPopupMenu();// 自己添加menu的Item

			addMenu = new JMenuItem("添加词库");
			addMenu.addActionListener(this);

			refreshMenu = new JMenuItem("刷新列表");
			refreshMenu.addActionListener(this);

			mainMenu.add(addMenu);
			mainMenu.add(refreshMenu);
			mainMenu.show(lblTip, e.getX(), e.getY());
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}
